#pragma once
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string>
#include <iostream>


class InetAddr
{
public:
    InetAddr(const struct sockaddr_in& addr)
        : _addr(addr)
        , _port(ntohs(addr.sin_port))
    {
        _ip.resize(INET_ADDRSTRLEN);
        if(inet_ntop(AF_INET, &addr.sin_addr, const_cast<char*>(_ip.c_str()), _ip.size()) == nullptr)
        {
            std::cerr << "网络字节序转主机序失败！" << std::endl;
            exit(0);
        }
    }

    std::string ip()
    {
        return _ip;
    }

    uint16_t port()
    {
        return _port;
    }

    struct sockaddr_in& addr()
    {
        return _addr;
    }

    bool operator==(const InetAddr& addr)
    {
        return this->_ip == addr._ip && this->_port == addr._port;
    }   

    ~InetAddr(){}

private:
    uint16_t _port;
    std::string _ip;
    struct sockaddr_in _addr;
};